package com.school.service.impl;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.school.dao.HljtDutyDao;
import com.school.dao.HljtSchoolManagerDao;
import com.school.entity.HljtDuty;
import com.school.entity.HljtSchoolimage;
import com.school.entity.web.HljtSchoolSynopsis;
import com.school.service.HljtSchoolManagerService;
import com.school.util.MapperToJson;
import com.school.util.RepMessage;
import com.school.util.WhetherNotToUpdate;

@Service
public class HljtSchoolManagerServiceImpl implements HljtSchoolManagerService{
	
	@Autowired
	HljtSchoolManagerDao oHljtSchoolManagerDao;
	
	@Autowired
	HljtDutyDao hljtDutyDao;
	
	
	@Override
	public String findImageInfo(int nType) {
		String strPath = "";
		RepMessage oRepMessage=new RepMessage();
		switch (nType) {
		case 1:
			List<HljtSchoolSynopsis> sList=new ArrayList<HljtSchoolSynopsis>();
			sList=oHljtSchoolManagerDao.findSchoolInfo(nType);
			
			if(sList.size()>0){
				try {
					strPath=WhetherNotToUpdate.getIp()+sList.get(0).getImagePath();
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				sList.get(0).setImagePath(strPath);
			}
			oRepMessage.setObjData(sList);
			break;
		case 4:
			List<HljtSchoolimage> list=new 	ArrayList<HljtSchoolimage>();
			 list=	oHljtSchoolManagerDao.findSchoolImageByType(nType);
			 if(list.size()>0){
					try {
						strPath=WhetherNotToUpdate.getIp()+list.get(0).getImagepath();
					} catch (UnknownHostException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					list.get(0).setImagepath(strPath);
				}
			oRepMessage.setObjData(list);
			break;
		default:
			break;
		}
		oRepMessage.setFlag(true);
		return MapperToJson.toJson(oRepMessage);
	}

	@Override
	public String saveSchoolInfo(HljtSchoolSynopsis oHljtSchoolSynopsis) {
		RepMessage  oRepMessage=new RepMessage();
		int nflag=oHljtSchoolManagerDao.selectSchoolCount(1);
		// nCount;
		if(nflag==0){
			this.insert(null, oHljtSchoolSynopsis);
			oRepMessage.setFlag(true);
			
		}else{
			int nCount=oHljtSchoolManagerDao.saveSchoolInfo(oHljtSchoolSynopsis);
			if(nCount!=0){
				oRepMessage.setFlag(true);
			}else{
				oRepMessage.setFlag(false);
			}
			oRepMessage.setObjData(nCount);;
		}
		
		return MapperToJson.toJson(oRepMessage);
	}

	@Override
	public String saveImageInfo(HljtSchoolimage oHljtSchoolimage) {
		RepMessage  oRepMessage=new RepMessage();
		int nFlag=oHljtSchoolManagerDao.selectSchoolCount(Integer.parseInt(oHljtSchoolimage.getType()));
		if(nFlag==0){
			this.insert(oHljtSchoolimage, null);
		}
	
		int nCount = oHljtSchoolManagerDao.saveHljtSchoolimage(oHljtSchoolimage);
		if(nCount!=0){
			oRepMessage.setFlag(true);
		}else{
			oRepMessage.setFlag(false);
		}
		oRepMessage.setObjData(nCount);;
		return MapperToJson.toJson(oRepMessage);
	}
	
	public String insert(HljtSchoolimage oHljtSchoolimage,HljtSchoolSynopsis oHljtSchoolSynopsis){
		
		if(oHljtSchoolimage==null &oHljtSchoolSynopsis!=null){
			
			oHljtSchoolimage=new HljtSchoolimage();
			oHljtSchoolimage.setSchoolid(1);
			oHljtSchoolimage.setType("1");
			oHljtSchoolimage.setImagepath(oHljtSchoolSynopsis.getImagePath());
			oHljtSchoolimage.setImagename("学校");
			oHljtSchoolManagerDao.insertSchoolInfo(oHljtSchoolimage);
			this.saveSchoolInfo(oHljtSchoolSynopsis);
		}else if(oHljtSchoolimage!=null &oHljtSchoolSynopsis==null){
			oHljtSchoolManagerDao.insertSchoolInfo(oHljtSchoolimage);
		}
		return "insert";
		
	}
	
	public String findAllHljtDutyInfo(String strPath,int pageIndex,int pageSize){
	pageIndex=(pageIndex-1)*pageSize;
	List<HljtDuty>  list=hljtDutyDao.findAllByPage(pageIndex, pageSize);
	for (HljtDuty hljtDuty : list) {
		hljtDuty.setDutyimage(strPath+hljtDuty.getDutyimage());
	}
	int nCount=hljtDutyDao.findCount();
	RepMessage oRepMessage=new RepMessage();
	oRepMessage.setFlag(true);
	oRepMessage.getObjMap().put("info", list);
	oRepMessage.getObjMap().put("sum", nCount);
	return MapperToJson.toJson(oRepMessage);
	}

	
}











